package leetcode_2501_2600;

public class LeeCode_2555 {
    public static void main(String[] args) {
        System.out.println(maximizeWin(new int[]{1, 1, 2, 2, 3, 3, 5}, 2));
        System.out.println(maximizeWin(new int[]{1, 2, 3, 4}, 0));
    }
    private static int maximizeWin(int[] prizePositions, int k) {
        int left = 0, n = prizePositions.length, ans = 0;
        int[] pre = new int[n + 1];
        for (int i = 0; i < n; i++) {
            while (prizePositions[i] - prizePositions[left] > k){
                left++;
            }
            ans = Math.max(ans, i - left + 1 + pre[left]);
            pre[i + 1] = Math.max(pre[i], i - left + 1);
        }
        return ans;
    }
}
